<template>
  <el-form :inline="true" style="float:right;">
    <el-form-item>
      <el-button size='mini' @click="search" type="primary">刷新</el-button>
    </el-form-item>

    <el-form-item v-if="globalFilter.date && globalFilter.date.enable">
      <el-date-picker
        v-model="globalFilter.date.date_range"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        format="YYYY-MM-DD"
        :default-value="new Date()"
        :shortcuts="shortcuts"
        size="mini"
      >
      </el-date-picker>
    </el-form-item>

  </el-form>
</template>

<script>
import {store} from "@/components/chart/common/store";

const shortcuts = [
  {
    text: '今天',
    value: (() => {
      const end = new Date()
      const start = new Date()
      return [start, end]
    })(),
  }, {
    text: '昨天',
    value: (() => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24)
      end.setTime(end.getTime() - 3600 * 1000 * 24)
      return [start, end]
    })(),
  }, {
    text: '最近一周',
    value: (() => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
      return [start, end]
    })(),
  }, {
    text: '最近一个月',
    value: (() => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
      return [start, end]
    })(),
  }, {
    text: '最近三个月',
    value: (() => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
      return [start, end]
    })(),
  }, {
    text: '去年',
    value: (() => {
      const year = new Date().getFullYear() - 1;
      const start = new Date(year + '-01-01')
      const end = new Date(year + '-12-31')
      return [start, end]
    })(),
  }, {
    text: '年初至今',
    value: (() => {
      // 获取去年的开始和结束时间
      const year = new Date().getFullYear();
      const start = new Date(year + '-01-01')
      const end = new Date()
      return [start, end]
    })(),
  }]

export default {
  name: "globalFilter",
  data() {
    return {
      shared: store,
      shortcuts: shortcuts
    }
  },
  computed: {
    globalFilter() {
      return this.dashboard.globalFilter
    }
  },
  props: {
    dashboard: {
      type: Object,
      default: {}
    }
  },
  methods: {
    search() {
      this.shared.globalFilter = this.globalFilter
      this.shared.dashboard = this.dashboard
      this.shared.runHash++;
    }
  }
}
</script>

<style scoped>
.el-form-item {
  margin-bottom: 0px !important;
}
</style>
